package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;

/**
 * Created by hx_Alex on 2018/5/31.
 */

public class SearchBaiduInfoReceiver extends BaseReceiver {

    private AccessibilityNodeInfo nodeInfo;
    private boolean isSearchBaiduMainBtn=false;
    private boolean isSearchBaiduMainSearch=false;
    public SearchBaiduInfoReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="搜索百度内容命令";
    }
    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }

    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }


    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("SearchBaiduInfoReceiver","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
        if(!isSearchBaiduMainSearch)
        {
            isSearchBaiduMainSearch= searchManger.searchBaiduMainSearchView(nodeInfo);
        }
        
        if(!isSearchBaiduMainBtn)
        {
           isSearchBaiduMainBtn= searchManger.searchBaiduMainBtnSearchView(nodeInfo);
        }
        
        if(isSearchBaiduMainSearch && isSearchBaiduMainBtn){
        commandCompleteListener.commandCompleteListener();
        isSearchBaiduMainBtn=false;
        isSearchBaiduMainSearch=false;
        }
        }
    
    
    public static boolean setSearchContent(AccessibilityNodeInfo node,String text)
    {
        if (Build.VERSION.SDK_INT >= 21) {
            //android>=21 = 5.0时可以用ACTION_SET_TEXT
            Bundle arg = new Bundle();
            arg.putString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, text);
            return node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arg);
        }
        return false;
    }
}
